package book_management.book;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * 书架类,所有书籍在这个类中存储
 */
public class BookList<usedSize> {
    // 使用JDK内置的动态数组
    // 保存每个书籍对象
    private static List<Book> books = new ArrayList<>();
    // 保存所有的书籍名称，作为操作书籍的索引
    private static List<String> booksName = new ArrayList<>();

    static {
        // 初始化操作
        books.add(new Book("水浒传","施耐庵",
                66.6,"武侠小说"));
        books.add(new Book("三国演义","罗贯中",
                88.6,"谋略小说"));
        books.add(new Book("西游记","吴承恩",
                99.6,"玄幻小说"));
        books.add(new Book("红楼梦","曹雪芹",
                260.6,"牛逼小说"));


        booksName.add("水浒传");
        booksName.add("三国演义");
        booksName.add("西游记");
        booksName.add("红楼梦");
    }


    /**
     * 根据书籍名称判断当前书架上是否存在该书
     * @param booksName
     * @return
     */
    public boolean isContains(String booksName) {
        // ArrayList.contains();
        if (this.booksName.contains(booksName)) {
            return true;
        }else {
            return false;
        }
    }

    public void addBook(Book book) {
        books.add(book);
    }


    public void printBook() {
        // 遍历Books数组
        Iterator<Book> iterator = books.iterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            System.out.println(book);
        }
    }

    public static List<Book> getBooks() {
        return books;
    }

    public static void setBooks(List<Book> books) {
        BookList.books = books;
    }

    public void findBook(String name){
        Iterator<Book> iterator = books.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Book book = books.get(i);
            if(book.getName().equals(name)){
                book = books.get(i);
                System.out.println(book);
                break;
            }
            i++;
        }
    }
    public void borrowBook(String name){
        Iterator<Book> iterator = books.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Book book = books.get(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println(book);
                break;
            }
            i++;
        }
    }

    public void returnBook(String name){
        Iterator<Book> iterator = books.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Book book = books.get(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println(book);
                break;
            }
            i++;
        }
    }

    public void removeBook(String name){
        Iterator<Book> iterator = books.iterator();

        int i = 0;
        while (iterator.hasNext()) {
            Book book = books.get(i);
            if(book.getName().equals(name)){
                books.remove(i);
                break;
            }
            i++;
        }
    }

    public void updateBook(String name){
        Iterator<Book> iterator = books.iterator();
        int i = 0;
        Book book = books.get(i);
        Scanner scanner = new Scanner(System.in);
        while (iterator.hasNext()){
            book = iterator.next();
            if(book.getName().equals(name)){
                System.out.println("请输入你要修改的信息:1.更新价格 2.更新类型 3.更新状态");
                int choice = scanner.nextInt();

                if(choice == 1){
                    System.out.println("请输入新的价格：");
                    double newPrice = scanner.nextDouble();
                    book.setPrice(newPrice);
                }else if(choice == 2){
                    System.out.println("请输入新的类型：");
                    String newType = scanner.next();
                    book.setType(newType);
                }else if(choice == 3){
                    System.out.println("请输入更新后的借阅状态");
                    Boolean newBorrow = Boolean.valueOf(scanner.next());
                    book.setBorrowed(newBorrow);
                }
            }
            ++i;
        }
    }

}
